package com.apestech.formwork.condition;

import com.apestech.framework.db.SqlSession;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.sql.SqlUtil;
import com.apestech.framework.sql.callback.SaveTableCallback;
import com.apestech.framework.sql.callback.SaveTablesCallback;
import com.apestech.framework.xml.condition.modul.Account;
import com.apestech.framework.xml.condition.modul.Condition;

import java.util.ArrayList;

/**
 * @author hup
 * @create 2019-01-30-19:13
 */
public class ExecAccountExcute extends SqlUtil implements SaveTableCallback{

    private Account account;
    private Account.Bill bill;
    private SaveTablesCallback saveTablesCallback;
    private String executeId;

    public ExecAccountExcute(Account account, Account.Bill bill, String executeId, SaveTablesCallback saveTablesCallback){
        this.account=account;
        this.bill=bill;
        this.executeId=executeId;
        this.saveTablesCallback=saveTablesCallback;
    }

    @Override
    public boolean before(String tabName) throws Exception {
        return true;
    }

    @Override
    public void write(String tabName, JSONObject input, int index, SqlSession session) throws Exception {
        Account.UpdateTable updateTable=bill.getUpdateTable(tabName);
        if(updateTable!=null){
            String update=updateTable.getBillTableName();
            String keyField=updateTable.getKeyField();
            JSONObject kmap=bill.getTable().getIdDataMap(tabName, input);
            JSONObject param=new JSONObject();
            param.putAll(input);
            JSONArray qlist=session.getForJSON().table(update, kmap);
            param.putAll(qlist.get(0));
            String idValue=param.getString(keyField);
            if(idValue!=null) idValue=idValue.trim();
            if(idValue!=null && !"".equals(idValue)){
                ExecTable execTable=account.getConditon().getExecTable(idValue);
                ArrayList<String> fields=account.getUpdateField();
                int len=fields.size();
                String timeField=session.getSpecialField().getTimestamp();
                String sql=" UPDATE "+execTable.getTabname()+" SET "+timeField+"=#{"+timeField+"_NEW} ";
                String where=" WHERE 1=1 AND ID=#{"+keyField+"} AND "+timeField+"=#{"+timeField+"} "+execTable.getKeyWhere();
                for(int i=0; i<len; i++){
                    Account.Update uf=account.getUpdate(fields.get(i));
                    ArrayList<String> vlist=updateTable.getExecute(executeId).getVarList();
                    int vlen=vlist.size();
                    String s=uf.getResult();
                    String t=uf.getTrigger();
                    if(t!=null) t=t.trim();
                    for(int v=0; v<vlen; v++){
                        s=s.replace("{"+vlist.get(v)+"}", updateTable.getExecute(executeId).getVarExpress(vlist.get(v)));
                        if(t!=null)
                            t=t.replace("{"+vlist.get(v)+"}", updateTable.getExecute(executeId).getVarExpress(vlist.get(v)));
                    }
                    sql=sql+","+uf.getField()+"="+s;
                    if(t!=null && !"".equals(t))
                        where=where+" AND "+t;
                }
                sql=sql+where;
                Condition.ExecTableData execTableData=account.getConditon().getExecTableData(idValue, param);
                JSONObject oldData=execTableData.getList().get(0);
                if(oldData==null) throw new Exception("帐目已经被修改或者："+bill.getMsg());
                param.put(timeField, oldData.get(timeField));
                param.put(timeField+"_NEW", "SYSDATE");
                if(session.getForUpdate().update(sql,param)==0) throw new Exception("帐目已经被修改或者："+bill.getMsg());
            }
        }
    }

    @Override
    public String after(String tabName, JSONObject input) throws Exception {
        return null;
    }
}
